package com.threegroup.web.controller;

import com.threegroup.common.dto.OrdersDTO;
import com.threegroup.common.dto.OrdersPageDTO;
import com.threegroup.common.utils.Result;
import com.threegroup.common.utils.UserUtils;
import com.threegroup.web.annotation.Login;
import com.threegroup.web.service.OrdersService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.websocket.Session;

@RestController
public class OrdersController {
    @Resource
    private OrdersService ordersService;
    @Resource
    private UserUtils userUtils;

    //生成订单号(token) 幂等性问题
    @Login
    @GetMapping("mp/orders/number")
    public Result getOrdersNamber(){

        Long userId = userUtils.getUserId();

        Result result = ordersService.getOrdersNamber(String.valueOf(userId));

        return result;
    }

    //提交订单
    @Login
    @PostMapping("mp/orders/submit")
    public Result submitOrder(HttpServletRequest request,@RequestBody OrdersDTO ordersDTO) {
        //从已经登录的作用域里取出用户id
        Long userId = userUtils.getUserId();
        Result result = ordersService.addOrders(ordersDTO,userId);
        return result;
    }

    //重复提交订单
    @PostMapping("mp/orders/again")
    public Result againOrder(@RequestBody OrdersDTO ordersDTO) {

        //从已经登录的作用域里取出用户id
        Long userId = userUtils.getUserId();
        Result result = ordersService.addOrders(ordersDTO,userId);

        return result;
    }

    //删除订单
    @DeleteMapping("mp/orders")
    public Result deleteOrderDetail(@RequestBody Long[] ids) {
        Result result = ordersService.deleteOrders(ids);
        return result;
    }

    //分页查询订单
    @Login
    @GetMapping("mp/orders/page")
    public Result selectOrderDetailList(OrdersPageDTO ordersPageDTO){
        Result result = ordersService.selectOrderDetailList(ordersPageDTO);
        return result;
    }

    //修改订单状态
    @PostMapping("mp/orders/update")
    public Result updateOrders(@RequestBody OrdersDTO ordersDTO){
        Result result = ordersService.updeteOrders(ordersDTO);
        return result;
    }

}
